﻿using System;
using System.Net.Mail;
using System.Text;
using NENAData.Errors;
using NENAData.LINQ;
using NENAData.NENAProvinces;
using NENAData.Transactions;

namespace NENAData.Email
{
    public class Attachment
    {
        public static void Newsletter(ProvinceList pProvinces, TransactionList.Transactions pTransactionType,
            string strFrom, string strSubject, string strMessage,
            string strAttachmentLocation1, string strAttachmentLocation2)
        {
            MailMessage oMessage = new MailMessage();
            oMessage.From = new MailAddress(strFrom);
            oMessage.To.Add(new MailAddress(strFrom));

            AffiliateMembers oAffiliateMembers = new AffiliateMembers();
            var omembers = oAffiliateMembers.PaidAffiliateMembers(pProvinces, pTransactionType);

            foreach (var oIterate in omembers)
            {
                oMessage.Bcc.Add(new MailAddress(oIterate.EmailAddress, oIterate.FirstName + " " + oIterate.LastName));
            }
            oMessage.Bcc.Add(new MailAddress("websiteadministrator@nena.ca"));
            oMessage.Subject = strSubject;
            oMessage.Body = strMessage;
            oMessage.IsBodyHtml = true;
            if (strAttachmentLocation1 != string.Empty)
                oMessage.Attachments.Add(new System.Net.Mail.Attachment(strAttachmentLocation1));

            if (strAttachmentLocation2 != string.Empty)
                oMessage.Attachments.Add(new System.Net.Mail.Attachment(strAttachmentLocation2));

            //SmtpClient client = new SmtpClient("oldwebsite.nena.ca", 25);
            //client.Credentials = new System.Net.NetworkCredential("pronychgary", "garyrqhr");
            //SmtpClient client = new SmtpClient("rarsk.com", 25);
            //client.Credentials = new System.Net.NetworkCredential("gpronych", "garyrhd");
            SmtpClient client = new SmtpClient();

            try
            {
                client.Send(oMessage);
            }
            catch (Exception ex)
            {
                //unable to send email
                throw ex;
            }
        }

    }
}
